package cn.com.sueframe.upload.action;

import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;

import javax.imageio.ImageIO;

import cn.com.sueframe.action.BaseActionSupport;
import cn.com.sueframe.common.IDGenerator;
import cn.com.sueframe.service.FileUploadService;
import cn.com.sueframe.vo.FileVO;

/*******************************************************************************
 * 上传文件处理器
 * 
 * @author anmin Create Oct 6, 2012
 */
public class UploadFileAction extends BaseActionSupport {
	private static final long serialVersionUID = 1L;

	private File file;

	private String fileFileName;

	private FileUploadService fileUploadService;

	/**
	 * 保存文件，将文件信息保存到指定的目录中，同时将文件相对目录保存到数据库中去，以便在页面中可以访问图片
	 * 
	 * @throws IOException
	 * @author anmin Create 2012-10-6
	 */
	public void saveFile() throws IOException {
		init();
		try {
			String fileDir = "/dm/image/";
			//图片后缀
			String filePost = fileFileName.substring(fileFileName.indexOf(".") + 1);  
			String fileName = fileDir + IDGenerator.gerateUUID() + "." + filePost;
			processFile(file,fileName,filePost);
			saveFileToDataBase();
			pw.write("y");
		} catch (Exception e) {
			pw.write("n");
			e.printStackTrace();
		}
		pw.close();
	}
	/**
	 * 用来处理图片文件，将图片文件的像素变小
	 * @author anmin
	 * Create Nov 3, 2012
	 * @param file
	 * @param fileName
	 * @param filePost
	 */
	private void processFile(File file ,String fileName,String filePost){
		try {
            File fi = file; //大图文件
            File fo = new File(fileName); //将要转换出的小图文件
            AffineTransform transform = new AffineTransform();
            BufferedImage bis = ImageIO.read(fi);
            int w = bis.getWidth();
            int h = bis.getHeight();
            //double scale = (double)w/h;
            int nw = w;
            int nh = h;
            //double sx = (double)nw / w;
            //double sy = (double)nh / h;
            transform.setToScale(1,1);
            AffineTransformOp ato = new AffineTransformOp(transform, null);
            BufferedImage bid = new BufferedImage(nw, nh, BufferedImage.TYPE_3BYTE_BGR);
            ato.filter(bis,bid);
            ImageIO.write(bid, filePost, fo);
        } catch(Exception e) {
            e.printStackTrace();
        }
	}

	/**
	 * 将文件相对目录保存到数据库中去，以便在页面中可以访问图片
	 * 
	 * @author anmin Create 2012-10-6
	 * @throws SQLException 
	 */
	private void saveFileToDataBase() throws SQLException {
		FileVO fileVO = new FileVO();
		fileVO.setId(IDGenerator.gerateUUID());
		fileVO.setFileName(fileFileName);
        fileUploadService.saveFileToDataBase(fileVO);
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	public FileUploadService getFileUploadService() {
		return fileUploadService;
	}

	public void setFileUploadService(FileUploadService fileUploadService) {
		this.fileUploadService = fileUploadService;
	}
}
